In [1]:
import numpy as np
import numpy.random
import matplotlib.pyplot as plt
# Generate some test data
x = np.random.randn(8873)
y = np.random.randn(8873)
heatmap, xedges, yedges = np.histogram2d(x, y, bins=50)
extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]
plt.clf()
plt.imshow(heatmap.T, extent=extent, origin='lower')
plt.show()
The previous bit of code is to make sure that the Heatmap plots in my Python 3 notebook.
In [38]:
import numpy as np
import numpy.random
import matplotlib.pyplot as plt
import csv
aortaData = []
aa1 = []
aa2 = []
aa3 = []
aa4 = []
aa5 = []
with open ("genomicdata.csv") as csvfile:
readCSV = csv.reader(csvfile, delimiter= '\t') #gives access to the CSV file
for col in readCSV:
if col[7] == '':
aortaData.append('0')
else:
aortaData.append(col[7]) #save the data from the side
#need smaller arrays
#Each array needs 5 elements
aa1 = list(map(float, aortaData[1:6]))
aa2 = list(map(float, aortaData[6:11]))
aa3 = list(map(float, aortaData[11:16]))
aa4 = list(map(float, aortaData[16:21]))
aa5 = list(map(float, aortaData[21:26]))
# Generate some test data
x = np.random.randn(8873)
y = np.random.randn(8873)
heatmap, xedges, yedges = np.histogram2d(x, y, bins=5) #if array is 5x5 (has 25 components), need 5 bins
extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]
plt.clf()
plt.imshow(heatmap.T, extent=extent, origin='lower')
plt.colorbar()
plt.show()
In the code above, I created a heatmap using the code from the heatmap above ( http://stackoverflow.com/questions/2369492/generate-a-heatmap-in-matplotlib-using-a-scatter-data-set ) and from the data of some of the expression vectors for genes in cells in the aorta.
In [28]:
import matplotlib.pyplot as plt
import numpy as np
aortaData = []
aa1 = []
aa2 = []
aa3 = []
aa4 = []
aa5 = []
with open ("genomicdata.csv") as csvfile:
readCSV = csv.reader(csvfile, delimiter= '\t') #gives access to the CSV file
for col in readCSV:
if col[7] == '':
aortaData.append('0')
else:
aortaData.append(col[7]) #save the data from the side
#need smaller arrays
#Each array needs 5 elements
aa1 = list(map(float, aortaData[1:6]))
aa2 = list(map(float, aortaData[6:11]))
aa3 = list(map(float, aortaData[11:16]))
aa4 = list(map(float, aortaData[16:21]))
aa5 = list(map(float, aortaData[21:26]))
x = [1, 2, 3, 4, 5]
y = [0.1, 0.2, 0.3, 0.4, 0.5]
#take the 5 small arrays
intensity = [
aa1,
aa2,
aa3,
aa4,
aa5
]
#setup the 2D grid with Numpy
x, y = np.meshgrid(x, y)
#convert intensity (list of lists) to a numpy array for plotting
intensity = np.array(intensity)
plt.pcolormesh(x, y, intensity) #plots the intensity on the grid with colors
plt.colorbar() #colors are set from the colorbar. The colorbar shows the intensity
plt.show()
In [20]:
import matplotlib.pyplot as plt
import numpy as np
#here's our data to plot, all normal Python lists
x = [1, 2, 3, 4, 5]
y = [0.1, 0.2, 0.3, 0.4, 0.5]
intensity = [
[3, 0, 20, 2, .6],
[4, 33, 13, 2, 5],
[.6, 1, 9, 10, 10],
[0, 7, 0, 6, 4],
[18, 15, 94, 0, 0]
]
#setup the 2D grid with Numpy
x, y = np.meshgrid(x, y)
#convert intensity (list of lists) to a numpy array for plotting
intensity = np.array(intensity)
#now just plug the data into pcolormesh, it's that easy!
plt.pcolormesh(x, y, intensity)
plt.colorbar() #need a colorbar to show the intensity scale
plt.show() #boom
This is a test to manually see how the heat map should be expressed. I took the array from the previous example, and wrote it out manually to test how the data will be expressed. http://seaborn.pydata.org/generated/seaborn.heatmap.html
In [30]:
import matplotlib.pyplot as plt
import numpy as np
aortaData = []
aa1 = []
aa2 = []
aa3 = []
aa4 = []
aa5 = []
aa6 = []
aa7 = []
aa8 = []
aa9 = []
aa10 = []
with open ("genomicdata.csv") as csvfile:
readCSV = csv.reader(csvfile, delimiter= '\t') #gives access to the CSV file
for col in readCSV:
if col[7] == '':
aortaData.append('0')
else:
aortaData.append(col[7]) #save the data from the side
#need smaller arrays
#Each array needs 5 elements
aa1 = list(map(float, aortaData[1:11]))
aa2 = list(map(float, aortaData[11:21]))
aa3 = list(map(float, aortaData[21:31]))
aa4 = list(map(float, aortaData[31:41]))
aa5 = list(map(float, aortaData[41:51]))
aa6 = list(map(float, aortaData[51:61]))
aa7 = list(map(float, aortaData[61:71]))
aa8 = list(map(float, aortaData[71:81]))
aa9 = list(map(float, aortaData[81:91]))
aa10 = list(map(float, aortaData[91:101]))
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
#take the 10 small arrays
intensity = [
aa1,
aa2,
aa3,
aa4,
aa5,
aa6,
aa7,
aa8,
aa9,
aa10
]
#setup the 2D grid with Numpy
x, y = np.meshgrid(x, y)
#convert intensity (list of lists) to a numpy array for plotting
intensity = np.array(intensity)
plt.pcolormesh(x, y, intensity) #plots the intensity on the grid with colors
plt.colorbar() #colors are set from the colorbar. The colorbar shows the intensity
plt.show()
In [ ]: